/**
  *****************************************************************************
  * @file    Libraries/Device/TS32F020X/startup/arm/ts32f020x_startup.c
  * @author  JUSHENG Application Team
  * @version V1.0.0
  * @date    02-19-2022
  * @brief   CMSIS Cortex-M0 Core Peripheral Access Layer Source File for 
  *          JUSHENG TS32F020X MCU
  ****************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2022 JUSHENG</center></h2>
  *
  *
  *
  ******************************************************************************
  */ 

#include "ts32f020x.h"
#include "include.h"
/* Macro Definitions */
#if defined(__CC_ARM)                /* KEIL Compiler */
#define WEAK            __attribute__ ((weak))
#define ALIAS(f)        __attribute__ ((weak, alias(#f)))
#define WEAK_ALIAS_FUNC(FUN, FUN_ALIAS) \
    void FUN(void) __attribute__ ((weak, alias(#FUN_ALIAS)));

#elif defined(__GNUC__)              /* GCC Compiler */
#define WEAK            __attribute__ ((weak))
#define ALIAS(f)        __attribute__ ((weak, alias(#f)))
#define WEAK_ALIAS_FUNC(FUN, FUN_ALIAS) \
    void FUN(void) __attribute__ ((weak, alias(#FUN_ALIAS)));

#elif defined (__ICCARM__)           /* IAR Compiler */
#define WEAK_ALIAS_FUNC(FUN, FUN_ALIAS) \
void FUN(void);                         \
_Pragma(_STRINGIFY(_WEAK_ALIAS_FUNC(FUN, FUN_ALIAS)))
#define _WEAK_ALIAS_FUNC(FUN, FUN_ALIAS) weak __WEAK_ALIAS_FUNC(FUN, FUN_ALIAS)
#define __WEAK_ALIAS_FUNC(FUN, FUN_ALIAS) FUN##=##FUN_ALIAS
#endif

/* Initialize segments */
#if defined(__CC_ARM)                /* KEIL Compiler */
extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit;
extern void __main(void);

#elif defined(__GNUC__)              /* GCC Compiler */
extern uint32_t __StackTop;
extern uint32_t __etext;
extern uint32_t __data_start__;
extern uint32_t __data_end__;
extern uint32_t __bss_start__;
extern uint32_t __bss_end__;
extern uint32_t __bss_extern_start__  WEAK;
extern uint32_t __bss_extern_end__ WEAK;

extern void uvisor_init(void);
extern void software_init_hook(void) __attribute__((weak));
extern void __libc_init_array(void);
extern int main(void);

#elif defined (__ICCARM__)           /* IAR Compiler */
void __iar_program_start(void);

#endif
void HardFault_Handler(void);
/* Default empty handler */
void Default_Handler(void);

void NMI_Handler(void);

/* Reset handler */
void Reset_Handler(void);

/* Cortex-M0 core handlers */
WEAK_ALIAS_FUNC(NMI_Handler,                                                Default_Handler)
WEAK_ALIAS_FUNC(HardFault_Handler,                                          Default_Handler)
WEAK_ALIAS_FUNC(SVC_Handler,                                                Default_Handler)
WEAK_ALIAS_FUNC(PendSV_Handler,                                             Default_Handler)
WEAK_ALIAS_FUNC(SysTick_Handler,                                            Default_Handler)

/* Peripherals handlers */
WEAK_ALIAS_FUNC(LVD_IRQHandler,                                             Default_Handler)
WEAK_ALIAS_FUNC(TKIRQ_IRQHandler,                                           Default_Handler)
WEAK_ALIAS_FUNC(TKSCDN_IRQHandler,                                          Default_Handler)
WEAK_ALIAS_FUNC(TKOVFL_IRQHandler,                                          Default_Handler)
WEAK_ALIAS_FUNC(UART0_IRQHandler,                                           Default_Handler)
WEAK_ALIAS_FUNC(UART1_IRQHandler,                                           Default_Handler)
WEAK_ALIAS_FUNC(UST_IRQHandler,                                             Default_Handler)
WEAK_ALIAS_FUNC(SPI0_IRQHandler,                                            Default_Handler)
WEAK_ALIAS_FUNC(GPIOA_IRQHandler,                                           Default_Handler)
WEAK_ALIAS_FUNC(GPIOB_IRQHandler,                                           Default_Handler)
WEAK_ALIAS_FUNC(WKPND_IRQHandler,                                           Default_Handler)
WEAK_ALIAS_FUNC(TIM0_IRQHandler,                                            Default_Handler)
WEAK_ALIAS_FUNC(TIM1_IRQHandler,                                            Default_Handler)
WEAK_ALIAS_FUNC(TIM4_IRQHandler,                                            Default_Handler)
WEAK_ALIAS_FUNC(ADC_IRQHandler,                                             Default_Handler)
WEAK_ALIAS_FUNC(CRC_DMA_IRQHandler,                                         Default_Handler)
WEAK_ALIAS_FUNC(WDT_IRQHandler,                                             Default_Handler)
WEAK_ALIAS_FUNC(LCD_IRQHandler,                                             Default_Handler)


typedef void (*FUNC_IRQ)(void);

/* Vector table */
#if defined(__CC_ARM)           /* KEIL Compiler */
__attribute__((section("RESET")))
const FUNC_IRQ __vector_handlers[] = {
#elif defined(__GNUC__)         /* GCC Compiler */
__attribute__((section(".vector_table")))
const FUNC_IRQ __vector_handlers[] = {
#elif defined (__ICCARM__)      /* IAR Compiler */
extern uint32_t CSTACK$$Limit;
const FUNC_IRQ __vector_table[] @ ".intvec" = {
#endif

#if defined(__CC_ARM)           /* KEIL Compiler */
    (FUNC_IRQ)&Image$$ARM_LIB_STACK$$ZI$$Limit,
#elif defined(__GNUC__)         /* GCC Compiler */
    (FUNC_IRQ)&__StackTop,
#elif defined(__ICCARM__)       /* IAR Compiler */
    (FUNC_IRQ)&CSTACK$$Limit,
#endif

    Reset_Handler,              // Reset Handler
    NMI_Handler,                // NMI Handler
    HardFault_Handler,          // Hard Fault Handler
    0,                          // Reserved
    0,                          // Reserved
    0,                          // Reserved
    0,                          // Reserved
    0,                          // Reserved
    0,                          // Reserved
    0,                          // Reserved
    SVC_Handler,                // SVCall Handler
    0,                          // Reserved
    0,                          // Reserved
    PendSV_Handler,             // PendSV Handler
    SysTick_Handler,            // SysTick Handler

    /* External Interrupts */
    LVD_IRQHandler,             //0
    0,                          //1
    TKSCDN_IRQHandler,          //2
    0,                          //3
    UART0_IRQHandler,           //4
    UART1_IRQHandler,           //5
    UST_IRQHandler,             //6
    SPI0_IRQHandler,            //7
    GPIOA_IRQHandler,           //8
    GPIOB_IRQHandler,           //9
    WKPND_IRQHandler,           //10
    TIM0_IRQHandler,            //11
    TIM1_IRQHandler,            //12
    TIM4_IRQHandler,            //13
    ADC_IRQHandler,           //14
    CRC_DMA_IRQHandler,         //15
    WDT_IRQHandler,             //16
    LCD_IRQHandler,             //17
//    0,
//    0,
//    0,
//    0,
//    0,
//    0,
//    0,
//    0,
//    0,
//    0,
};

extern void SystemInit(void);
extern int main(void);
void Reset_Handler(void)
{
    SystemInit();

#if defined(__CC_ARM)           /* KEIL Compiler */
   __main();
#elif defined(__ICCARM__)       /* GCC Compiler */
    __iar_program_start();
#elif defined(__GNUC__)         /* IAR Compiler */
    uint32_t *src_ind = (uint32_t *) &__etext;
    uint32_t *dst_ind = (uint32_t *) &__data_start__;
    uint32_t *dst_end = (uint32_t *) &__data_end__;

    /* Move .data section from ROM to RAM */
    if(src_ind != dst_ind) {
        for(; dst_ind < dst_end;) {
            *dst_ind ++ = *src_ind ++;
        }
    }

    /* Initialize .bss section to zero */
    dst_ind = (uint32_t *) &__bss_start__;
    dst_end = (uint32_t *) &__bss_end__;
    if(dst_ind != dst_end) {
        for(; dst_ind < dst_end;) {
            *dst_ind ++ = 0;
        }
    }

    /* Initialize .bss.extern section to zero */
    dst_ind = (uint32_t *) &__bss_extern_start__;
    dst_end = (uint32_t *) &__bss_extern_end__;
    if(dst_ind != dst_end) {
        for(; dst_ind < dst_end;) {
            *dst_ind ++ = 0;
        }
    }

    /* uvisor_init();  */

    if(software_init_hook) {
        /**
         * Give control to the RTOS via software_init_hook() which will also call __libc_init_array().
         * Assume software_init_hook() is defined in libraries/rtos/rtx/TARGET_CORTEX_M/RTX_CM_lib.h.
         */
        software_init_hook();
    } else {
        __libc_init_array();
        main();
    }
#endif
    /* Infinite loop */
    while(1);
}


void Default_Handler(void)
{
    while(1);
}

/*************************** (C) COPYRIGHT 2022 JUSHENG ***** END OF FILE *****/
